package com.gaussian.property.app.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaussian.property.app.vo.AppProjectReport;
import com.gaussian.property.module.biz.entity.ProjectReportFile;
import com.gaussian.property.module.biz.entity.ProjectReportFileDownloadRecord;
import com.gaussian.property.module.biz.mapper.ProjectReportFileMapper;
import com.gaussian.property.module.biz.service.IProjectReportFileDownloadRecordService;
import com.gaussian.property.module.biz.vo.projectreport.PageQueryProjectReportRequest;
import com.gaussian.property.module.biz.vo.projectreport.ProjectReportFileVO;
import com.songqi.common.core.domain.page.PageDataInfo;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/8/11 22:41
 */
@Service
public class ProjectReportService {
    @Resource
    private ProjectReportFileMapper projectReportFileMapper;
    @Resource
    private IProjectReportFileDownloadRecordService iProjectReportFileDownloadRecordService;

    public PageDataInfo<AppProjectReport> pageQueryProjectReport(PageQueryProjectReportRequest queryRequest, String appUserId) {
        IPage<ProjectReportFileVO> iPage = MyBatisUtils.buildPage(queryRequest);
        projectReportFileMapper.pageQueryProjectReportJoinDownloadRecord(iPage, queryRequest, appUserId);
        PageDataInfo<AppProjectReport> reportPage = new PageDataInfo<>(iPage);

        if(CollUtil.isEmpty(iPage.getRecords())){
            return reportPage;
        }
        List<ProjectReportFileVO> records = iPage.getRecords();
        Map<String, List<ProjectReportFileVO>> collect = records.stream().collect(Collectors.groupingBy(ProjectReportFileVO::getMonth));

        List<AppProjectReport> appProjectReportList = new ArrayList<>();

        for(Map.Entry<String, List<ProjectReportFileVO>> entry : collect.entrySet()){
            AppProjectReport appProjectReport = new AppProjectReport();
            appProjectReport.setMonth(entry.getKey());
            appProjectReport.setProjectReportFileVoList(entry.getValue());
            appProjectReport.setCount(appProjectReport.getProjectReportFileVoList().size());
            appProjectReportList.add(appProjectReport);
        }
        reportPage.setRows(appProjectReportList);
        return reportPage;
    }

    public PageDataInfo<AppProjectReport> pageQueryProjectReportDownloadRecord(PageQueryProjectReportRequest queryRequest, String appUserId) {

        IPage<ProjectReportFileVO> iPage = MyBatisUtils.buildPage(queryRequest);
        projectReportFileMapper.pageQueryProjectReportDownloadRecord(iPage, queryRequest, appUserId);
        PageDataInfo<AppProjectReport> reportPage = new PageDataInfo<>(iPage);

        if(CollUtil.isEmpty(iPage.getRecords())){
            return reportPage;
        }
        List<ProjectReportFileVO> records = iPage.getRecords();
        Map<String, List<ProjectReportFileVO>> collect = records.stream().collect(Collectors.groupingBy(ProjectReportFileVO::getMonth));

        List<AppProjectReport> appProjectReportList = new ArrayList<>();

        for(Map.Entry<String, List<ProjectReportFileVO>> entry : collect.entrySet()){
            AppProjectReport appProjectReport = new AppProjectReport();
            appProjectReport.setMonth(entry.getKey());
            appProjectReport.setProjectReportFileVoList(entry.getValue());
            appProjectReport.setCount(appProjectReport.getProjectReportFileVoList().size());
            appProjectReportList.add(appProjectReport);
        }
        reportPage.setRows(appProjectReportList);
        return reportPage;
    }


    public void markDownload(String projectReportFileId, String appUserId) {
        ProjectReportFile projectReportFile = projectReportFileMapper.selectById(projectReportFileId);
        if(projectReportFile == null){
          throw new CustomException("项目报告id不存在");
        }
        LambdaQueryWrapper<ProjectReportFileDownloadRecord> queryWrapper = new LambdaQueryWrapper<ProjectReportFileDownloadRecord>()
                .eq(ProjectReportFileDownloadRecord::getProjectReportFileId, projectReportFileId)
                .eq(ProjectReportFileDownloadRecord::getAppUserId, appUserId);
        ProjectReportFileDownloadRecord downloadRecord = iProjectReportFileDownloadRecordService.getOne(queryWrapper, false);
        if(downloadRecord == null){
            downloadRecord = new ProjectReportFileDownloadRecord();
        }
        downloadRecord.setProjectReportFileId(projectReportFileId);
        downloadRecord.setAppUserId(appUserId);
        iProjectReportFileDownloadRecordService.saveOrUpdate(downloadRecord);

    }


    /**
     * 删除下载记录
     * @param projectReportFileDownloadRecordId
     */
    public void deleteDownloadRecord(String projectReportFileDownloadRecordId) {
        iProjectReportFileDownloadRecordService.removeById(projectReportFileDownloadRecordId);
    }


}
